package com.gwt.tirso.gymkhana.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginMainUI extends Composite {
	
	private Button logoutBt = new Button();
	ILoginServiceAsync loginService;

	public LoginMainUI(String name) {
		this.initWidget(this.createContentPanel(name));
	}

	private Widget createContentPanel(String name) {
		VerticalPanel logPanel = new VerticalPanel();
		logoutBt.setText("Desconectarse");
		
		logPanel.add(new Label("¡Bienvenido "+name+"!"));
		logPanel.add(logoutBt);

		logPanel.addStyleName("login");
		
		this.logoutBt.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				loginService = ConnectionRpc.initRpcLogin();
				
				loginService.logout(new AsyncCallback() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Object result) {
						Window.alert("Desconectado correctamente");
						RootPanel.get("login_widget").clear();
						Login l = new Login();
						l.onModuleLoad();
					}
				});
				
			}
		});
		
		//applying style
		logPanel.addStyleName("loginCon");
		
		return logPanel;
	}

}
